package cn.qqncn.furniture.controller;

import cn.qqncn.furniture.pojo.domain.Category;
import cn.qqncn.furniture.pojo.domain.Furniture;
import cn.qqncn.furniture.pojo.vo.PieVo;
import cn.qqncn.furniture.pojo.vo.ShopVo;
import cn.qqncn.furniture.service.CategoryService;
import cn.qqncn.furniture.service.FurnitureService;
import cn.qqncn.furniture.util.OSSTool;
import cn.qqncn.furniture.util.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.*;

/**
 * @author zkw
 * @ClassName FurnitrueController.java
 * @Description TODO 家具控制层
 * @createTime 2020年12月09日 08:41:00
 */
@Controller
@CrossOrigin
@RequestMapping("/furniture")
public class FurnitureController {

    @Autowired
    FurnitureService furnitureService;

    static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 分页查询-接口
     *
     * @param page
     * @param size
     * @param post_search
     * @param min
     * @param max
     * @return
     * @throws JsonProcessingException
     */
    @ResponseBody
    @GetMapping("/getAll")
    public String getAll(@PathParam("page") int page,
                         @PathParam("size") int size,
                         @PathParam("post_search") String post_search,
                         @PathParam("min") Integer min,
                         @PathParam("max") Integer max) throws JsonProcessingException {
        List<ShopVo> list = furnitureService.pageSelect(page, size, post_search, min, max);
        System.out.println("*******" + list);
        PageInfo<ShopVo> info = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap();
        map.put("list", list);
        map.put("total", info.getTotal());
        String value = objectMapper.writeValueAsString(map);
        return value;
    }


    /**
     * 查看商品详情-Controller
     *
     * @param modelAndView
     * @param furnitureId
     * @return
     */
    @RequestMapping("/details")
    public ModelAndView details(ModelAndView modelAndView, Integer furnitureId) {
        Furniture furniture = furnitureService.getFurnitureById(furnitureId);
        modelAndView.addObject("furniture", furniture);
        modelAndView.setViewName("/front/product-details");
        return modelAndView;
    }

    /**
     * 商品更新
     *
     * @param dropzFile
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Map<String, String> upload(MultipartFile dropzFile, HttpServletRequest request) {
        String fileUrl = OSSTool.uploadFile(UUID.randomUUID().toString(), dropzFile);
        Map<String, String> map = new HashMap<>();
//        返回文件OSS的URL
        map.put("fileName", fileUrl);
        return map;
    }

    //商品添加
    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody Furniture furniture) throws JsonProcessingException {
        System.out.println(furniture);
        Map<String, Object> map = new HashMap<>();
        Integer i = furnitureService.addFurniture(furniture);
        if (i <= 0 || i == null) {
            map.put("flag", false);
            return objectMapper.writeValueAsString(map);
        } else {
            map.put("flag", true);
            return objectMapper.writeValueAsString(map);
        }
    }

    /**
     * 查询商品列表
     *
     * @return
     */
    @GetMapping("back_getAll")
    @ResponseBody
    public String resultSet() throws JsonProcessingException {
        List<ShopVo> list = furnitureService.getAllVo();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", list.size());
        map.put("data", list);
        return objectMapper.writeValueAsString(map);
    }

    @PostMapping("update")
    @ResponseBody
    public String update(@RequestBody Furniture furniture) throws JsonProcessingException {
        ObjectNode objectNode = objectMapper.createObjectNode();
        int i = furnitureService.updateById(furniture);
        if (i > 0) {
            objectNode.put("code", "00000");
            return objectMapper.writeValueAsString(objectNode);
        } else {
            objectNode.put("code", "B0001");
            return objectMapper.writeValueAsString(objectNode);
        }

    }


    /**
     * 批量删除
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("delete")
    @ResponseBody
    public String delete(@RequestBody String id) throws JsonProcessingException {
        String substring = id.substring(id.indexOf('[')+1, id.indexOf(']'));
        String str[] = substring.split(",");
        int[] arr = StringUtils.stringToIntArrays(str);
        int i = 0;
        if(arr.length>0){
            i = furnitureService.deleteSelectFurniture(arr);
            return result(i);
        }
        return result(i);
    }

    /**
     * 返回值封装
     * @param i sql执行成功条数
     * @return
     * @throws JsonProcessingException
     */
    public static String result(int i) throws JsonProcessingException {
        ObjectNode objectNode = objectMapper.createObjectNode();
        if (i > 0) {
            objectNode.put("code", "00000");
            return objectMapper.writeValueAsString(objectNode);
        } else {
            objectNode.put("code", "B0001");
            return objectMapper.writeValueAsString(objectNode);
        }
    }

    /**
     * 根据类别查询商品 跳转
     * @param categoryId
     * @return
     */
    @RequestMapping("getByCategory")
    public ModelAndView getByCategory(ModelAndView modelAndView,Integer categoryId){
        modelAndView.setViewName("/front/shop2");
        List<ShopVo> list = furnitureService.selectByCategoryId(categoryId);
        System.out.println(list);
        modelAndView.addObject("list",list);
        return modelAndView;
    }

    @ResponseBody
    @GetMapping("getAdminPie")
    public String getAdminPie() throws JsonProcessingException {
        List<PieVo> pieVos = furnitureService.selectPieVo();
        List<String> list = new ArrayList<>();
        for (PieVo pieVo : pieVos) {
            list.add(pieVo.getName());
        }
        HashMap<String,List> map = new HashMap<>();
        map.put("pieVos",pieVos);
        map.put("names",list);
        return objectMapper.writeValueAsString(map);
    }
}

